//
//  Bomb.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 8/16/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "Bomb.h"
#import "Utility.h"
#import "DefineXML.h"
#import "SimpleAudioEngine.h"

@implementation Bomb
@synthesize _state = mState;

-(id) init
{
    if (self = [super initWithSpriteFrameName:[self getSpriteFrameName]])
    {
        mState = eBS_IDLE;
        [self setIgnoreBodyRotation:NO];
        [self createBody];
        [self setColor:CL_BOMB];
    }
    
    return self;
}

-(void) explode
{
    // dispatch event
    [[NSNotificationCenter defaultCenter] postNotificationName:EVENT_BOMB_EXPLOSION object:nil];
    
    // play sound
    [[Utility getInstance] playEffect:SOUND_BOMB_EXPLOSION];
    mState = eBS_EPLOSION;
    [self stopAllActions];
    [self setColor:ccc3(255, 255, 255)];
    
    CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:SFN_EXPLOSION];
    [self setDisplayFrame:spriteFrame];
    
    CCAction *anim = [[Utility getInstance] getAnimWithKey:XML_ANIM_BOMB delay:0.03 loop:1];
    CCCallFunc *eplodeEnd = [CCCallFunc actionWithTarget:self selector:@selector(explosionEnd)];
    CCSequence *sequence = [CCSequence actionOne:(CCFiniteTimeAction *)anim two:eplodeEnd];
    
    [self runAction:sequence];
    [self schedule:@selector(updateExplosionBody)];
}

-(void) explosionEnd
{
    CCLOG(@"explosion end");
    mState = eBS_DEAD;
    [self setVisible:NO];
    [self unschedule:@selector(updateExplosionBody)];
}

-(void) createBody
{
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(0, 0);
    
    _b2Body = PhysicSystem::getInstance()->mWorld->CreateBody(&bodyDef);
    
    b2CircleShape shape;
    shape.m_radius = (self.contentSize.width / 2) / PTM_RATIO;
    
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &shape;
    fixtureDef.filter.categoryBits = [self getObjectType];
    fixtureDef.filter.maskBits = MASK_BIT_BOMB;
    fixtureDef.isSensor = true;
    _b2Body->CreateFixture(&fixtureDef);
    
    [self setPTMRatio:PTM_RATIO];
    _b2Body->SetUserData(self);
}

-(void) updateExplosionBody
{
    _b2Body->DestroyFixture(_b2Body->GetFixtureList());
    b2CircleShape shape;
    shape.m_radius = (self.contentSize.width / 2) / PTM_RATIO;
    
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &shape;
    fixtureDef.filter.categoryBits = [self getObjectType];
    fixtureDef.filter.maskBits = MASK_BIT_BOMB;
    fixtureDef.isSensor = true;
    _b2Body->CreateFixture(&fixtureDef);
}

-(OBJECT_TYPE) getObjectType
{
    
    return eO_BOMB;
}

-(void) addActions:(NSArray *)actions
{
    mListAction = [actions retain];
}

-(void) startAction
{
//    CCLOG(@"start action");
    if (mListAction != nil)
    {
        for (int i = 0; i < mListAction.count; i++)
        {
            [self runAction:[mListAction objectAtIndex:i]];
        }
    }
}

-(void) stopAction
{
    [self stopAllActions];
}

-(NSString *) getSpriteFrameName
{
    return SFN_BOMB;
}

-(void) dealloc
{
    [self stopAllActions];
    
    [mListAction release];
    mListAction = nil;
    
    [super dealloc];
    CCLOG(@"Bomb dealloc!");
}

@end
